package net.lzzy.lxq.controller;

import net.lzzy.lxq.entities.AdminUser;
import net.lzzy.lxq.entities.UserBean;
import net.lzzy.lxq.entities.UserDevice;
import net.lzzy.lxq.server.AdminUserService;
import net.lzzy.lxq.server.UserDeviceService;
import net.lzzy.lxq.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    UserService service;
    @Autowired
    UserDeviceService deviceService;
    @Autowired
    AdminUserService adminUserService;

    @PostMapping("/personnel_login")
    public Map<String,Object> personnelLogin(@RequestBody Map<String,String> param){
        //UserBean userBean=service.getUser(param.get("id"),param.get("password"));
        UserBean userBean=verifyUser(param);
        Map<String,Object> resultMap=new HashMap<>();
        if (userBean==null){
            resultMap.put("code",1);
            return resultMap;
        }
        UserDevice userDevice=deviceService.getUserDevice(userBean.getId());
        if (userDevice==null){
            resultMap.put("code",2);
            return resultMap;
        }
        if (param.get("deviceId")==null){
            resultMap.put("code",1);
            return resultMap;
        }else if (!param.get("deviceId").equals(userDevice.getDeviceId())){
            resultMap.put("code",3);
            return resultMap;
        }

        resultMap.put("code",0);
        resultMap.put("name",userBean.getName());
        return resultMap;
    }

    @PostMapping("/bind_device")
    public Map<String,Object> bindDevice(@RequestBody Map<String,String> map){
        UserBean userBean=verifyUser(map);
        Map<String,Object> resultMap=new HashMap<>();
        if (userBean==null){
            resultMap.put("code",1);
            return resultMap;
        }
        //deviceService.bindDevice(map.get("deviceId"),map.get("id"));
        UserDevice device=new UserDevice();
        device.setDeviceId(map.get("deviceId"));
        device.setId(0);
        device.setUserId(map.get("id"));
        deviceService.bindDevice(device);
        /*device=deviceService.getUserDevice(map.get("id"));
        if (!device.getDeviceId().equals(map.get("deviceId"))){
            resultMap.put("code",1);
            return resultMap;
        }*/
        resultMap.put("code",0);
        return resultMap;
    }

    @GetMapping("/get_users")
    public List<UserBean> getAll(){
        return service.getAll();
    }

    private UserBean verifyUser(Map<String, String> map){
        UserBean userBean=service.getUser(map.get("id"),map.get("password"));
        if (userBean==null){
            return null;
        }
        return userBean;
    }

    //@PostMapping("/admin_login")
    public Map<String,Object> adminLogin(@RequestBody Map<String,String> map){
        AdminUser adminUser=adminUserService.getAdminUser(map.get("account"),map.get("password"));
        Map<String,Object> result=new HashMap<>();
        if (adminUser==null){
            result.put("code",1);
            result.put("msg","账号或密码错误");
        }else{
            result.put("code",0);
            result.put("msg","登录成功");
            result.put("redirect","admin/admin.html");
        }
        return result;
    }
}
